<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>133-数组去重案例-分析过程</title>
    <script>
        // 数组去重（重点案例）
        // 有一个数组 ['c','a','z','a','x','a','x','c','b']，要求去重数组中重复的元素。
        //          0    1    2    3    4    5    6    7    8
        var arr = ['c', 'a', 'z', 'a', 'x', 'a', 'x', 'c', 'b'];
        // temp = 'c', 0 newArr = ['c']
        // temp = 'a', 1 newArr = ['c','a']
        console.log(arr);
        var buChongFuArray = [];
        // 1 【c】
        buChongFuArray.push(arr[0]);
        var num1 = arr.indexOf(buChongFuArray[0], 1);
        console.log("index: " + num1);
        var newArr2 = [];
        for (var i = 0; i < num1; i++) {
            newArr2.push(arr[i]);
        }
        for (var i = num1 + 1; i < arr.length; i++) {
            newArr2.push(arr[i]);
        }
        arr = newArr2;
        console.log(arr);
        console.log("不：" + buChongFuArray);
        // 2-1 【c,a】
        buChongFuArray.push(arr[1]);
        var num1 = arr.indexOf(buChongFuArray[0], 1);
        console.log("index: " + num1);
        var newArr2 = [];
        for (var i = 0; i < num1; i++) {
            newArr2.push(arr[i]);
        }
        for (var i = num1 + 1; i < arr.length; i++) {
            newArr2.push(arr[i]);
        }
        arr = newArr2;
        console.log(arr);
        console.log("不：" + buChongFuArray)
        // 2-2 【c,a】
        var num1 = arr.indexOf(buChongFuArray[1], 2);
        console.log("index: " + num1);
        var newArr2 = [];
        for (var i = 0; i < num1; i++) {
            newArr2.push(arr[i]);
        }
        for (var i = num1 + 1; i < arr.length; i++) {
            newArr2.push(arr[i]);
        }
        arr = newArr2;
        console.log(arr);
        console.log("不：" + buChongFuArray)
        // 2-3 【c,a】
        var num1 = arr.indexOf(buChongFuArray[1], 2);
        console.log("index: " + num1);
        var newArr2 = [];
        for (var i = 0; i < num1; i++) {
            newArr2.push(arr[i]);
        }
        for (var i = num1 + 1; i < arr.length; i++) {
            newArr2.push(arr[i]);
        }
        arr = newArr2;
        console.log(arr);
        console.log("不：" + buChongFuArray)
        // 2-4 【c,a】
        var num1 = arr.indexOf(buChongFuArray[1], 2);
        console.log("index: " + num1);
        var newArr2 = [];
        for (var i = 0; i < num1; i++) {
            newArr2.push(arr[i]);
        }
        for (var i = num1 + 1; i < arr.length; i++) {
            newArr2.push(arr[i]);
        }
        arr = newArr2;
        console.log(arr);
        console.log("不：" + buChongFuArray)
        // 3 【c,a,z】
        buChongFuArray.push(arr[2]);
        var num1 = arr.indexOf(buChongFuArray[2], 3);
        console.log("index: " + num1);
        var newArr2 = [];
        for (var i = 0; i < num1; i++) {
            newArr2.push(arr[i]);
        }
        for (var i = num1 + 1; i < arr.length; i++) {
            newArr2.push(arr[i]);
        }
        arr = newArr2;
        console.log(arr);
        console.log("不：" + buChongFuArray)
        // 4-1 【c,a,z,x】
        buChongFuArray.push(arr[3]);
        var num1 = arr.indexOf(buChongFuArray[3], 4);
        console.log("index: " + num1);
        var newArr2 = [];
        for (var i = 0; i < num1; i++) {
            newArr2.push(arr[i]);
        }
        for (var i = num1 + 1; i < arr.length; i++) {
            newArr2.push(arr[i]);
        }
        arr = newArr2;
        console.log(arr);
        console.log("不：" + buChongFuArray)
        // 4-2 【c,a,z,x】
        var num1 = arr.indexOf(buChongFuArray[3], 4);
        console.log("index: " + num1);
        var newArr2 = [];
        for (var i = 0; i < num1; i++) {
            newArr2.push(arr[i]);
        }
        for (var i = num1 + 1; i < arr.length; i++) {
            newArr2.push(arr[i]);
        }
        arr = newArr2;
        console.log(arr);
        console.log("不：" + buChongFuArray)
        // 5-1 【c,a,z,x,b】
        buChongFuArray.push(arr[4]);
        var num1 = arr.indexOf(buChongFuArray[4], 5);
        console.log("index: " + num1);
        var newArr2 = [];
        for (var i = 0; i < num1; i++) {
            newArr2.push(arr[i]);
        }
        for (var i = num1 + 1; i < arr.length; i++) {
            newArr2.push(arr[i]);
        }
        arr = newArr2;
        console.log(arr);
        console.log("不：" + buChongFuArray);
        // 结束
    </script>
</head>

<body>
    <a href="133-test-自写-数组去重案例.html">最终代码</a>
</body>

</html>